package com.quectel.cms.controller.notice;

import cn.hutool.core.date.DateUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.notice.dto.NoticeActivityOutlinkDto;
import com.quectel.core.module.notice.service.NoticeActivityOutlinkService;
import com.quectel.util.common.POIUtils;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-20 11:35:25
 */
@RestController
@RequestMapping("noticeActivityOutlink")
@Tag(name = "NoticeActivityOutlinkController", description = "活动报名人员相关api")
public class NoticeActivityOutlinkController extends BaseController {

    @DubboReference
    private NoticeActivityOutlinkService noticeActivityOutlinkService;

    /**
     * 导出活动信息
     */
    @GetMapping("exportExcel")
    @RequirePermissions(values = "car:exportExcel")
    @Log(express = "导出活动报名人员信息")
    public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse hresp) {
        Request request = Request.configParamsNotPage(params);

        super.fillAuthorityData(params);

        List<NoticeActivityOutlinkDto> result = new ArrayList<>();
        int pageNo = 0;
        params.put("limit", 1000);
        while (true) {
            params.put("offset", pageNo++ * 1000);
            List<NoticeActivityOutlinkDto> list = noticeActivityOutlinkService.queryList(params);
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
                break;
            }
            result.addAll(list);
            if (list.size() < 1000) {
                break;
            }
        }
        String[] headers = new String[]{"报名人姓名", "报名人手机号", "报名时间"};
        List<String[]> dataList = new ArrayList<>();
        for (NoticeActivityOutlinkDto data : result) {
            String[] item = new String[headers.length];
            item[0] = data.getName();
            item[1] = data.getPhone();
            item[2] = DateUtil.format(data.getCreateTime(), "yyyy-MM-dd HH:mm:ss");
            dataList.add(item);
        }
        POIUtils.exportExcel(hresp, "报名信息", headers, dataList, "报名信息");

    }

    /**
     * 活动报名人员查询分页列表
     */
    @GetMapping("list")
//    @RequirePermissions(values ="noticeActivityOutlink:list")
    @Operation(summary = "活动报名人员查询分页列表")
    public Response<Page<NoticeActivityOutlinkDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));
        Long activityId = request.getLong("activityId");
        super.fillAuthorityData(request.getParams());
        request.put("activityId", activityId);
        int total = noticeActivityOutlinkService.queryTotal(request.getParams());

        List<NoticeActivityOutlinkDto> list = null;
        if (total > 0) {
            list = noticeActivityOutlinkService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<NoticeActivityOutlinkDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<NoticeActivityOutlinkDto>>ok().wrap(page);
    }


    /**
     * 查询活动报名人员单条记录
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "noticeActivityOutlink:info")
    @Operation(summary = "查询活动报名人员单条记录")
    public Response<NoticeActivityOutlinkDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (noticeActivityOutlinkService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        NoticeActivityOutlinkDto noticeActivityOutlinkDto = noticeActivityOutlinkService.selectById(id);

        return Response.<NoticeActivityOutlinkDto>ok().wrap(noticeActivityOutlinkDto);
    }

    /**
     * 保存活动报名人员单条记录
     */
    @PostMapping("save")
    @RequirePermissions(values = "noticeActivityOutlink:edit")
    @Operation(summary = "保存活动报名人员单条记录")
    public Response<Object> save(@RequestBody @Validated NoticeActivityOutlinkDto noticeActivityOutlinkDto) {

        //填充基础数据
        fillSaveCommonData(noticeActivityOutlinkDto);

        noticeActivityOutlinkService.save(noticeActivityOutlinkDto);

        return Response.ok();
    }

    /**
     * 修改活动报名人员单条记录
     */
    @PostMapping("update")
    @RequirePermissions(values = "noticeActivityOutlink:edit")
    @Operation(summary = "修改活动报名人员单条记录")
    public Response<Object> update(@RequestBody @Validated NoticeActivityOutlinkDto noticeActivityOutlinkDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(noticeActivityOutlinkDto.getId()));
        if (noticeActivityOutlinkService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(noticeActivityOutlinkDto);

        noticeActivityOutlinkService.updateById(noticeActivityOutlinkDto);

        return Response.ok();
    }

    /**
     * 批量删除活动详情记录
     */
    @PostMapping("delete")
    @RequirePermissions(values = "noticeActivityOutlink:delete")
    @Operation(summary = "批量删除活动报名人员记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (noticeActivityOutlinkService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        noticeActivityOutlinkService.deleteBatch(ids);

        return Response.ok();
    }
}
